package com.heima.article.service.impl;

import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticleService;
import com.heima.feigns.AdminFeign;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    private ApArticleMapper apArticleMapper;

    @Override
    public void computeHotArticle() {
        //获取当前日期前5天的文章,根据当前时间-5
        String date = LocalDateTime.now().minusDays(20).format(DateTimeFormatter.ofPattern("yyyy-MM-dd 00:00:00"));

        List<ApArticle> articleList = apArticleMapper.selectArticleByDate(date);

        //2 计算热点文章分值
        List<HotArticleVo> hotArticleVoList = computeArticleScore(articleList);

        //根据频道分类
        cacheTagToRedis(hotArticleVoList);


    }
    @Autowired
    AdminFeign adminFeign;

    private void cacheTagToRedis(List<HotArticleVo> hotArticleVoList) {
        //查询全部频道
        List<AdChannel> adChannels = adminFeign.findAll().getData();
        //遍历每一个频道
        for (AdChannel adChannel : adChannels) {
            //根据每一个频道判断是否有当前的数据
//            hotArticleVoList.stream().
        }
    }

    /**
     * 计算热点文章分值
     *
     * @param articleList
     * @return
     */
    private List<HotArticleVo> computeArticleScore(List<ApArticle> articleList) {

         return articleList.stream().map(apArticle -> {
             //创建vo对象用于封装分数
             HotArticleVo hotArticleVo = new HotArticleVo();
             BeanUtils.copyProperties(apArticle, hotArticleVo);
             //根据每一个对象计算对应的分值
             Integer score = computeScore(apArticle);
             hotArticleVo.setScore(score);
             return hotArticleVo;
             }).collect(Collectors.toList());
    }

    private Integer computeScore(ApArticle apArticle) {
        Integer score = 0;
        //根据文章对象判断每一个属性
        if (apArticle.getViews() != null && apArticle.getViews() !=0){
            score += apArticle.getViews() *1;
        }
        if (apArticle.getLikes() != null && apArticle.getLikes() != 0){
            score += apArticle.getLikes()*3;
        }
        if (apArticle.getComment() != null && apArticle.getComment() != 0){
            score += apArticle.getComment() *5;
        }
        if (apArticle.getCollection() != null && apArticle.getCollection() != 0){
            score += apArticle.getCollection() *8;
        }

        return score;
    }
}
